package com.xm.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import com.xm.entity.NewsType;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.xm.dao.NewsTypeDaoImpl;
import com.wxm.servlet.BaseServlet;
import com.xm.dao.NewsDaoImpl;
import com.xm.entity.News;
import com.xm.uti.Result;

/**
 * Servlet implementation class NewsServletAdmin
 */
@WebServlet("/admin/NewsServletAdmin")
public class NewsServletAdmin extends BaseServlet {
  NewsDaoImpl moreDao = new NewsDaoImpl();
  
  /**
   * 获取新闻列表
   * @param req
   * @param resp
   * @throws ServletException
   * @throws IOException
   */
  public void getNewsList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	String title = req.getParameter("name");
	String newsTypeId = req.getParameter("typeId");
    String mindate = req.getParameter("mindate");
    String maxdate = req.getParameter("maxdate");
    String page = req.getParameter("page");
    String limit = req.getParameter("limit");
   
    ArrayList<News> moreList = moreDao.getNewsList(title,newsTypeId,mindate,maxdate,page,limit);
    
    ArrayList<News> moreList2= moreDao.getNewsList(title,newsTypeId,mindate,maxdate,null,null);
   
    String res = Result.toClient(0, "获取新闻列表", moreList, moreList2.size());
    
   
    PrintWriter out = resp.getWriter();
    out.write(res);
    out.flush();
    out.close();
  }
  /**
   * 添加
   * @param req
   * @param resp
   * @throws ServletException
   * @throws IOException
   */
  public void insertNews(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    //1.从页面上拿到数据
    String title = req.getParameter("title");//表单中标签nameֵ
    String content = req.getParameter("content");
    String newsTypeId = req.getParameter("typeId");
    String date = req.getParameter("date");
    String isShow = req.getParameter("isShow");
    int newsTypeIds=0;
    if(newsTypeId!=null && !newsTypeId.equals("")) {
      newsTypeIds=Integer.valueOf(newsTypeId);
    }
    //2、调用dao层方法获取数据添加到数据中
    News news = new News(0,title,content,newsTypeIds,date,isShow);
    boolean res = moreDao.insertNews(news);
    //3、讲数据返回到页面上
    PrintWriter out = resp.getWriter(); 
    if(res) {
      out.write(Result.toClient(0, "添加成功"));
    }else {
      out.write(Result.toClient(1, "添加失败"));
    }
    out.flush();//刷新
    out.close();//关闭
  }
  /**
   * 修改
   * @param request
   * @param response
   * @throws ServletException
   * @throws IOException
   */
  public void updateNews(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
	  
	String title = req.getParameter("title");
	String content = req.getParameter("content");
    String newsTypeId = req.getParameter("typeId");
    String date = req.getParameter("date");
    String isShow = req.getParameter("isShow");
    String id = req.getParameter("id");
    System.out.println(newsTypeId);
    int ids = 0;
    if(id!=null && !id.equals("")) {
      ids = Integer.valueOf(id);
    }
    int newsTypeIds=0;
    if(newsTypeId!=null && !newsTypeId.equals("")) {
      newsTypeIds=Integer.valueOf(newsTypeId);
    }
    
   
    News news = new News(ids,title,content,newsTypeIds,date,isShow);
    boolean res = moreDao.updateNews(news);

    PrintWriter out = resp.getWriter(); 
    if(res) {
      out.write(Result.toClient(0, "修改成功"));
    }else {
      out.write(Result.toClient(1, "修改失败"));
    }
    out.flush();
    out.close();
  }
  /**
   * 删除
   * @param req
   * @param resp
   * @throws ServletException
   * @throws IOException
   */
  public void delNews(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    //1.从页面上拿到数据
    String id = req.getParameter("id");
    
    int ids = 0;
    if(id!=null && !id.equals("")) {
      ids = Integer.valueOf(id);
    }
    
    //2、调用dao层方法获取数据删除
    boolean res =moreDao.delNews(ids);
    
    //3、讲数据返回到页面上
        PrintWriter out = resp.getWriter(); 
        if(res) {
          out.write(Result.toClient(0, "删除成功"));
        }else {
          out.write(Result.toClient(1, "删除失败"));
        }
        out.flush();//刷新
        out.close();//关闭
  }
  
}